Skip to content
This repository was archived by the owner on Jan 23, 2026. It is now read-only.

Add node-fetch patch for isAbortSignal#433

Closed
jhaynie wants to merge 1 commit intomainfrom
node-fetch-patch
Closed

Add node-fetch patch for isAbortSignal#433
jhaynie wants to merge 1 commit intomainfrom
node-fetch-patch

Conversation

@jhaynie
Copy link
Copy Markdown
Member

@jhaynie jhaynie commented Aug 14, 2025

Summary by CodeRabbit

  • Bug Fixes
    • Improves compatibility with node-fetch by correctly recognizing AbortSignal, reducing failures in request cancellation scenarios.
  • Refactor
    • Enhances the patching mechanism to better support both JavaScript and TypeScript modules.
    • Preserves async/export behavior and handles multiple function declaration styles for more reliable runtime behavior across environments.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Aug 14, 2025

Walkthrough

Adds a new Node-fetch-specific patch to modify isAbortSignal behavior, and significantly extends the bundler’s patching engine to handle JS/TS targets, export/async propagation, function vs const forms, arrow/function wrappers, argument handling, and loader selection.

Changes

Cohort / File(s) Summary
Node-fetch patch registration
internal/bundler/node-fetch.go
Registers a patch for module "node-fetch" targeting "src/utils/is". Adds an After hook for isAbortSignal to return true when original result is truthy or when first argument’s constructor.name is "AbortSignal". Executed during init via global patches map.
Patching engine enhancements
internal/bundler/patch.go
Enhances patching to differentiate JS vs TS, detect export/async, support function declarations and const assignments, generate appropriate wrappers (arrow for JS, function for TS), preserve modifiers, manage arguments (_args/arguments), route Before/After hooks, and select esbuild loader accordingly.

Sequence Diagram(s)

sequenceDiagram
  participant Caller
  participant NodeFetch_isAbortSignal as node-fetch:isAbortSignal (patched)
  participant OriginalFn as __agentuity_isAbortSignal (original)

  Caller->>NodeFetch_isAbortSignal: isAbortSignal(arg)
  NodeFetch_isAbortSignal->>OriginalFn: call with _args
  OriginalFn-->>NodeFetch_isAbortSignal: result
  alt After hook logic
    NodeFetch_isAbortSignal-->>Caller: true if result truthy or arg constructor.name==='AbortSignal'
  else
    NodeFetch_isAbortSignal-->>Caller: original result
  end
Loading
sequenceDiagram
  participant Patcher as Bundler Patcher
  participant Source as Source File (.js/.ts)
  participant Wrapper as Generated Wrapper
  participant Loader as esbuild Loader

  Patcher->>Source: Detect function or const assignment
  Patcher->>Source: Identify export/async + file type (JS/TS)
  Patcher->>Wrapper: Generate JS arrow or TS function wrapper
  Patcher->>Wrapper: Wire Before/After and call __agentuity_<fn>(..._args)
  Patcher->>Loader: Choose LoaderJS or LoaderTS
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

I tweaked the weave of patching thread,
With twitching nose, I forged ahead—
JS or TS, I hop between,
Exports, asyncs, glossy sheen.
AbortSignals? I sniff them true—
Thump-thump! A build now sleek and new.
(_/)> 🥕

Tip

🔌 Remote MCP (Model Context Protocol) integration is now available!

Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch node-fetch-patch

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🧹 Nitpick comments (3)
internal/bundler/patch.go (2)

104-104: Consider using path.Ext() for more robust file extension checking.

Using strings.HasSuffix for file extension checking could match unintended patterns (e.g., "myfile.notjs" would not match, but "myfilejs" without a dot would). Consider using path.Ext() for more reliable extension detection.

-isJS := strings.HasSuffix(args.Path, ".js")
+isJS := path.Ext(args.Path) == ".js"

108-116: Consider supporting other function declaration patterns.

The current implementation only handles function declarations and const declarations. Modern JavaScript/TypeScript also commonly uses let, var, and other patterns for function declarations. Consider expanding support for these patterns.

Would you like me to help implement support for additional function declaration patterns like let, var, or class methods?

internal/bundler/node-fetch.go (1)

8-14: Consider adding error handling for edge cases.

The patch assumes _args[0] exists and has a constructor property. Consider adding null/undefined checks to prevent potential runtime errors.

After: `if (result) { return true; }
-if (_args[0] && _args[0].constructor.name === 'AbortSignal') {
+if (_args && _args[0] && typeof _args[0] === 'object' && _args[0].constructor && _args[0].constructor.name === 'AbortSignal') {
    return true;
}
`,
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled

You can enable these settings in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between d7615cc and f1e9456.

📒 Files selected for processing (2)
  • internal/bundler/node-fetch.go (1 hunks)
  • internal/bundler/patch.go (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
  • GitHub Check: Build and Test (macos-latest)
  • GitHub Check: Analyze (go)
🔇 Additional comments (2)
internal/bundler/patch.go (1)

173-176: LGTM! Good language-specific loader selection.

The loader selection logic correctly differentiates between JS and TS files, ensuring proper parsing and transformation.

internal/bundler/node-fetch.go (1)

4-17: Confirm src/utils/is exists in your target node-fetch versions

The patch in internal/bundler/node-fetch.go uses

Filename: "src/utils/is",

but node-fetch typically stores this code in src/utils/is.js. Since node-fetch isn’t installed locally in your repo, please double-check against the versions you bundle (e.g. v2.x, v3.x) and ensure the path and extension are correct.

Points to verify:

  • internal/bundler/node-fetch.go
    • Does your node-fetch module actually include src/utils/is (no extension)?
    • If the file is src/utils/is.js (or .mjs), update the Filename field accordingly.

Comment on lines +9 to +13
After: `if (result) { return true; }
if (_args[0] && _args[0].constructor.name === 'AbortSignal') {
return true;
}
`,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consider defensive programming for constructor name check.

The constructor.name property can be minified or altered in production builds, making this check potentially unreliable. Consider adding additional checks or using instanceof if the AbortSignal class is available.

After: `if (result) { return true; }
-if (_args[0] && _args[0].constructor.name === 'AbortSignal') {
+if (_args[0] && (_args[0].constructor.name === 'AbortSignal' || (typeof AbortSignal !== 'undefined' && _args[0] instanceof AbortSignal))) {
    return true;
}
`,
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
After: `if (result) { return true; }
if (_args[0] && _args[0].constructor.name === 'AbortSignal') {
return true;
}
`,
After: `if (result) { return true; }
if (_args[0] && (_args[0].constructor.name === 'AbortSignal' || (typeof AbortSignal !== 'undefined' && _args[0] instanceof AbortSignal))) {
return true;
}
`,
🤖 Prompt for AI Agents
In internal/bundler/node-fetch.go around lines 9 to 13, the current check uses
_args[0].constructor.name === 'AbortSignal' which can break when names are
minified; replace it with a defensive sequence: first verify _args[0] is an
object, then if (typeof AbortSignal !== 'undefined' && _args[0] instanceof
AbortSignal) return true, otherwise fall back to duck-typing (e.g., check for
properties/methods like 'aborted' in _args[0] and typeof
_args[0].addEventListener === 'function') to reliably detect an AbortSignal
without relying on constructor.name.

Comment thread internal/bundler/patch.go
Comment on lines +135 to +136
if isExport {
fnprefix += "export " + fnprefix
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix incorrect string concatenation in export prefix.

The export prefix is being concatenated incorrectly. The current code produces "export async " when it should produce "export async ". This results in duplicating the async keyword.

-if isExport {
-    fnprefix += "export " + fnprefix
-}
+if isExport {
+    fnprefix = "export " + fnprefix
+}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if isExport {
fnprefix += "export " + fnprefix
if isExport {
fnprefix = "export " + fnprefix
}
🤖 Prompt for AI Agents
In internal/bundler/patch.go around lines 135 to 136, the code incorrectly
appends the current fnprefix to itself when adding the export keyword (fnprefix
+= "export " + fnprefix), causing duplication; replace that concatenation with a
simple prepend so that fnprefix is set to "export " + fnprefix (i.e., assign
rather than append) so the export prefix is added exactly once.

Comment thread internal/bundler/patch.go
Comment on lines +139 to +141
if isJS {
suffix.WriteString(fnprefix + fnname + "() => {\n")
suffix.WriteString("let args = arguments;\n")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix malformed arrow function syntax for JS targets.

The generated arrow function syntax is incorrect. Arrow functions should be declared as const fnname = () => {}, not function fnname() => {}.

-if isJS {
-    suffix.WriteString(fnprefix + fnname + "() => {\n")
-    suffix.WriteString("let args = arguments;\n")
+if isJS {
+    suffix.WriteString(fnprefix + fnname + "(...args) => {\n")

Also note that arrow functions don't have their own arguments object. The args parameter should be used directly.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if isJS {
suffix.WriteString(fnprefix + fnname + "() => {\n")
suffix.WriteString("let args = arguments;\n")
if isJS {
suffix.WriteString(fnprefix + fnname + "(...args) => {\n")
}
🤖 Prompt for AI Agents
In internal/bundler/patch.go around lines 139 to 141, the generated arrow
function syntax is malformed and incorrectly uses arguments; replace the current
output that writes fnprefix + fnname + "() => {" and the subsequent "let args =
arguments;" with a proper arrow function declaration that captures parameters,
e.g. write "const <fnname> = (...args) => {" for JS targets and remove any use
of the legacy arguments object so the code uses the args parameter directly.

@jhaynie jhaynie closed this Aug 15, 2025
@jhaynie jhaynie deleted the node-fetch-patch branch August 15, 2025 16:35
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant